package com.intelligentmeterreadingsys.waterlist.copywater.activity;


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;

import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.povo.ResourcesPoVo;
import com.intelligentmeterreadingsys.util.FileNameUtil;
import com.intelligentmeterreadingsys.waterlist.copywater.db.CopyWaterDao;




import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * 照相机
 * @author king
 *
 */
public class MyCaremaActivity extends Activity{

	 public String waternumber;
	 
	 public Button button;
	 
	 public Bitmap bitmap;
	 
	 public Button button1;
	 
	 public ImageView imageview;
	 public int i=0;//当前第几张图片
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mycarema);
		imageview=(ImageView)findViewById(R.id.MyimageView);
		button=(Button)findViewById(R.id.mycarma_ok_button);
		button1=(Button)findViewById(R.id.mycarma_agin_button);
		Intent intent=getIntent();
        Bundle bundle=intent.getExtras();
        String otherid=bundle.getString("otherid");
        CopyWaterDao dao=new CopyWaterDao(MyCaremaActivity.this);
        List<ResourcesPoVo>  list= dao.queryResour(otherid,"2");
		button1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
	                startActivityForResult(intent, 1);
				
			}
		});
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				  FileOutputStream b = null;
				   FileNameUtil f=new FileNameUtil(MyCaremaActivity.this);
				   
		            String fileName =f.Orderimgpath()+"/"+waternumber+".jpg";
		            try {
						b = new FileOutputStream(fileName);
						 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
                         CopyWaterDao dao=new CopyWaterDao(MyCaremaActivity.this);
                        // dao.saveWater(waternumber, null, fileName, null);
                         dao.close();
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				
			}
		});
		
		  
		
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		

		
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode==RESULT_OK){
			 String sdStatus = Environment.getExternalStorageState();
			 if(!sdStatus.equals(Environment.MEDIA_MOUNTED)){
				Toast.makeText(MyCaremaActivity.this, "SD卡不可用", Toast.LENGTH_LONG).show();
				 return ;
			 }
			 Bundle bundle = data.getExtras();
	             bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据，并转换为Bitmap图片格式
	          

	            ((ImageView) findViewById(R.id.MyimageView)).setImageBitmap(bitmap);// 将图片显示在ImageView里

		}
	
	}
   
	public void viewImage(){
		
		
	}
}
